//
//  ZyUtils.swift
//  swift各类控件集锦
//
//  Created by iosdev1 on 2016/12/30.
//  Copyright © 2016年 zyy. All rights reserved.
//

import Foundation
import UIKit

//    汉字转拼音
func zyUtilsChineseToStringContainTone(hanzi:String) -> String {
    let mutableString = NSMutableString.init(string: hanzi)
    CFStringTransform(mutableString, nil, kCFStringTransformToLatin, false)
    let pinyin = String(mutableString).replacingOccurrences(of: " ", with: "")
    return pinyin
}

func zyUtilsChineseToStringNoneTone(hanzi:String) -> String {
    let mutableString = NSMutableString.init(string: hanzi)
//    必须先转成带音调的，在转成不带音调的
    CFStringTransform(mutableString, nil, kCFStringTransformToLatin, false)
    CFStringTransform(mutableString, nil, kCFStringTransformStripDiacritics, false)
    let pinyin = String(mutableString).replacingOccurrences(of: " ", with: "")
    return pinyin
}

///label高度自适应
///
/// - Parameters:
///   - content: 文字
///   - widthContent: 最大宽度
///   - font: 字体尺寸等
/// - Returns: 高度
func zyUtilsgetContentSizeHeightFont(content:String, widthContent:CGFloat, font:CGFloat) -> CGFloat {
    var size = CGRect()
    let size2 = CGSize(width: widthContent, height: 0)//设置label的最大宽度
    let font:UIFont = UIFont.systemFont(ofSize: font)
    let attribute = [NSFontAttributeName:font]
    size = content.boundingRect(with: size2, options: NSStringDrawingOptions.usesLineFragmentOrigin, attributes: attribute , context: nil);
    return size.size.height
}

///label宽度自适应
///
/// - Parameters:
///   - content: 文字
///   - heightContent: 最大高度
///   - attributes: 字体，行距等
/// - Returns: 高度
func zyUtilsgetContentSizeWidthFont(content:String, heightContent:CGFloat, font:CGFloat) -> CGFloat {
    let size2 = CGSize(width:0, height:heightContent)
    let font:UIFont = UIFont.systemFont(ofSize: font)
    let attribute = [NSFontAttributeName:font]
    let rect = content.boundingRect(with: size2, options: NSStringDrawingOptions.usesLineFragmentOrigin, attributes: attribute , context: nil);
    return rect.size.width
}

////检测网址中的汉字 转化成utf8
//func chineseToUTF8(chinese:String) -> String {
//    for i in 0 ..< chinese.lengthOfBytes(using: String.Encoding.utf8) {
//        let a:Int = Int((chinese as NSString).character(at: i))
//        if (a > 0x4e00) && (a < 0x9fff) {
//            let chineseStr:NSString = (chinese as NSString).substring(with: NSMakeRange(i, 1)) as NSString
//            let utfStr = (chineseStr as NSString).addingPercentEscapes(using: String.Encoding.utf8.rawValue)
//            
//        }
//    }
//}



















